package com.qf.service.impl;

import com.qf.entity.Car;
import com.qf.entity.Card;
import com.qf.entity.Seal;
import com.qf.entity.Task;
import com.qf.exception.OAException;
import com.qf.mapper.CarMapper;
import com.qf.mapper.CardMapper;
import com.qf.mapper.SealMapper;
import com.qf.mapper.TaskMapper;
import com.qf.service.AdminService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.Date;

@Service
@Slf4j
public class AdminServiceImpl implements AdminService {

    @Resource
    private CardMapper cardMapper;

    @Resource
    private SealMapper sealMapper;

    @Resource
    private CarMapper carMapper;

    @Resource
    private TaskMapper taskMapper;

    @Override
    @Transactional
    public void card(Card card) {
        int count = cardMapper.insertInfoCard(card);
        if (count!=1){
            log.info("【打印名片信息】保存申请失败，card={}",card);
            throw new OAException(199,"打印申请失败");
        }
        Task param = new Task();
        param.setApplyName(card.getRealName());
        param.setApplyTime(card.getAuditTime());
        param.setTaskId(5);
        param.setStatus(card.getAuditStatus());
        param.setUserId(13);
        param.setEventId(card.getCardId());
        param.setStatus(0);
        param.setApplyTime(new Date());
        int num = taskMapper.insert(param);
        if (num!=1){
            log.info("【打印名片信息】保存申请失败，card={}",card);
            throw new OAException(199,"打印申请失败");
        }
    }

    @Override
    @Transactional
    public void insertSeal(Seal seal) {

        int count =sealMapper.insertSeal(seal);
        if (count!=1){
            log.info("【申请用章信息】用章申请失败，seal={}",seal);
            throw new OAException(199,"用章申请失败");
        }
        Task param = new Task();
        param.setApplyName(seal.getRealName());
        param.setTaskId(4);
        param.setStatus(seal.getAuditStatus());
        param.setUserId(13);
        param.setEventId(seal.getSealId());
        param.setStatus(0);
        param.setApplyTime(new Date());
        int num = taskMapper.insert(param);
        if (num!=1){
            log.info("【申请用章信息】用章申请失败，seal={}",seal);
            throw new OAException(199,"用章申请失败");
        }

    }

    @Override
    @Transactional
    public void insertCar(Car car) {

        int count = carMapper.insertSelective(car);
        if (count!=1){
            log.info("【车辆信息】添加车辆失败，seal={}",car);
            throw new OAException(199,"添加车辆失败");
        }
    }
}
